package com.baizhi.service.impl;

import com.baizhi.dao.RecordDao;
import com.baizhi.entity.Record;
import com.baizhi.service.RecordService;
import com.baizhi.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RecordServiceImpl implements RecordService {
    private SqlSession session = null;
    private RecordDao dao = null;
    @Override
    public void insert(Record r) {
        try {
            session = MybatisUtil.openSession();
            dao = session.getMapper(RecordDao.class);
            dao.insert(r);
            session.commit();
        } catch (Exception e) {
            if(session!=null){
                session.rollback();
            }
            throw new RuntimeException(e);
        }finally{
            if(session!=null){
                MybatisUtil.close(session);
            }
        }
    }

    @Override
    public List<Record> selectByAuctionId(int id) {
        List<Record> list = null;
        try {
            session = MybatisUtil.openSession();
            dao = session.getMapper(RecordDao.class);
            list = dao.selectByAuctionId(id);
            session.commit();
        } catch (Exception e) {
            if(session!=null){
                session.rollback();
            }
            throw new RuntimeException(e);
        }finally{
            if(session!=null){
                MybatisUtil.close(session);
            }
        }
        return list;
    }

    @Override
    public void deleteByAuction(int id) {
        try {
            session = MybatisUtil.openSession();
            dao = session.getMapper(RecordDao.class);
            dao.deleteByAuctionId(id);
            session.commit();
        } catch (Exception e) {
            if(session!=null){
                session.rollback();
            }
            throw new RuntimeException(e);
        }finally{
            if(session!=null){
                MybatisUtil.close(session);
            }
        }
    }
}
